var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [],
    showType: 0,
    page:0,
    limit:10,
    state:1,
    total:0.00,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
           // 页面初始化 options为页面跳转所带来的参数
    let that = this
    try {
      var tab = wx.getStorageSync('tab');
      this.setData({
        showType: tab,
      });
    } catch (e) {}
    this.getOrderList();
  },
  switchTab: function(event) {
    let showType = event.currentTarget.dataset.index;
    this.setData({
      showType: showType,
      orderList:[],
      page:0,
    });
    this.getOrderList();
  },
  getOrderList() {
    let that = this;
    let userId = wx.getStorageSync("userId");
    util.request(api.myRevenue, {
      showType: that.data.showType,
      userId:userId,
      page:that.data.page,
      size:that.data.limit,
    }).then(function(res) {
      console.log(res);
      if (res.errno === 0) {
        that.setData({total:res.data.allMoney});
        if(res.data.incomeList.length <= 0){
          that.setData({state:0});
        }
        if(res.data.incomeList.length <= that.data.limit){
          that.setData({state:0});
        }
        // console.log(res.data);
        var originOrderList = that.data.orderList;
        var newOrderList = originOrderList.concat(res.data.incomeList);
        that.setData({
          orderList: newOrderList,
        })
        wx.hideLoading();
      }
    });
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getOrderList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    var self = this;
    var state = self.data.state;

    if (state > 0){
      wx.showLoading({
            title: '加载中...',
        });
        self.data.page = self.data.page + 1;
        self.getOrderList();
         wx.hideLoading();
    }
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})